//
//  ServiceDetailVCHelper.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/19.
//

import UIKit

class ServiceDetailVCHelper: NSObject {
    static let shared = ServiceDetailVCHelper()
    
    //MARK: - 只用于刷新数据
    func cacheData(closure: ((Bool)->Void)? = nil)
    {
        //set block
        let viewModel = ServcieDetailService()
        viewModel.setBlockAfterNetwork({ (result , _) -> Void in
            closure?(true)
        }) { (code, error) -> Void in
            closure?(false)
        }
            
        viewModel.getMyBidJobDetail(ServiceDetailModelHelper.shared.serviceDetailModel.serviceID)
    }
    func sendSMS(_ phone: String){
        let urlStri: String = APPConfig.SendSMSURL + phone
        if let url = URL(string: urlStri), UIApplication.shared.canOpenURL(url){
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }
    func checkRequiredWhenFinishJob()->Bool{
        var alertMsg: String = ""
        
        //******1.Checklist
        for checklistModel in ServiceDetailModelHelper.shared.serviceDetailModel.checklists {
            if checklistModel.is_required == "true" {
                let title = checklistModel.title
                switch checklistModel.type {
                    case ChecklistType.Bool.rawValue:
                        if checklistModel.answer == "" || checklistModel.answer == "false" {
                            alertMsg += "* " + title + " must be checked.\n"
                        }
                    case ChecklistType.Image.rawValue:
                        let realImageCount = checklistModel.images.count
                        if realImageCount == 0 {
                            alertMsg += "* " + title + " must be checked.\n"
                        }
                    case ChecklistType.Text.rawValue:
                        if checklistModel.answer == "" {
                            alertMsg += "* " + title + " must be checked.\n"
                        }
                    default:
                        break
                }
            }
        }
        if alertMsg.count > 0 {
            Tool().showAlertViewWithOneBtn(title: "Job can not be finished due to following tasks",message: alertMsg,messageTextAlignment: .left)
            return false
        }
        return true
        
    }
}
